package com.htfyun.ft.fragment.engineer;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.htfyun.ft.BaseActivity;
import com.htfyun.ft.R;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.serialport.ServoSingleton;
import com.htfyun.serialport.uartservo.AX12_ServoUpdate.AX12Update_Implementation;
import com.htfyun.serialport.uartservo.IServoControl;
import com.htfyun.serialport.uartservo.OnServoListener;
import com.htfyun.serialport.uartservo.ServoControlAttr;
import com.htfyun.serialport.uartservo.ServoErrorAttr;
import com.htfyun.serialport.uartservo.ServoInterestedInfo;
import com.xw.repo.BubbleSeekBar;

import java.io.File;
import java.util.List;

import androidx.annotation.Nullable;

public class AX12UpdateActivity extends BaseActivity implements AX12Update_Implementation.OnUpdateListener {

    private Button btnUpdate;

    private BubbleSeekBar seekBar;

    private final String UPDATE_BIN_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + "servo.bin";

    private Context mContext;

    private TextView txtVersionBefore;
    private TextView txtVersionAfter;

    private boolean isUpdated = false;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ax12update);

        mContext = this;

        txtVersionBefore = (TextView) findViewById(R.id.txtVersionBefore);
        txtVersionAfter = (TextView) findViewById(R.id.txtVersionAfter);

        seekBar = (BubbleSeekBar) findViewById(R.id.seekBar);

        btnUpdate = (Button) findViewById(R.id.btnUpdate);
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                isUpdated = false;
                ServoSingleton.INSTANCE.getServoControl().getFirmwareVersion();

                btnUpdate.setEnabled(false);

                DebugLog.e("aa UPDATE_BIN_PATH = " + UPDATE_BIN_PATH);
                AX12Update_Implementation.getInstance().startUpdate(new File(UPDATE_BIN_PATH), AX12UpdateActivity.this);

            }
        });

        findViewById(R.id.btnCheck).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ServoSingleton.INSTANCE.getServoControl().getFirmwareVersion();

            }
        });

        findViewById(R.id.btnForceStop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AX12Update_Implementation.getInstance().forceStopUpdate();
            }
        });
        findViewById(R.id.btnExit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ServoSingleton.INSTANCE.getServoControl().registerServoListener(new OnServoListener() {
            @Override
            public void onServoListener(List<ServoErrorAttr> errorAttrList, ServoControlAttr controlAttr, ServoControlAttr.Instruction instruction, ServoInterestedInfo info) {
                switch (controlAttr) {
                    case firmware_version:
                        updateFirmwareVersion();
                        break;
                }
            }
        });

        ServoSingleton.INSTANCE.getServoControl().getFirmwareVersion();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void updateFirmwareVersion() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (!isUpdated) {

                    String txt = mContext.getString(R.string.update_version_before, ServoSingleton.INSTANCE.getServoControl().getServoInterestedInfo().getFirmwareVersion());
                    txtVersionBefore.setText(txt);

                } else {

                    String txt = mContext.getString(R.string.update_version_after, ServoSingleton.INSTANCE.getServoControl().getServoInterestedInfo().getFirmwareVersion());
                    txtVersionAfter.setText(txt);

                }
            }
        });
    }

    @Override
    public void onUpdateListener(float percent) {

        final float p = percent;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                seekBar.setProgress(p);
            }
        });
    }

    @Override
    public void onUpdateError( AX12Update_Implementation.UpdateErrorAttr updateErrorAttr) {

        final AX12Update_Implementation.UpdateErrorAttr attr = updateErrorAttr;

        if (attr == AX12Update_Implementation.UpdateErrorAttr.update_ok) {
            isUpdated = true;
            ServoSingleton.INSTANCE.getServoControl().getFirmwareVersion();

        }

        DebugLog.e("onUpdateError , attr = " + attr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btnUpdate.setEnabled(true);

                String status = getString(R.string.update_status_ok);
                switch (attr) {
                    case update_failed:
                        status = getString(R.string.update_status_fail);
                        break;
                    case update_file_error:
                        status = getString(R.string.update_status_file_error);
                        break;
                    case update_ok:
                        default:
                            break;
                }

                String statusStr = getString(R.string.update_status, status);

                Toast.makeText(mContext, statusStr, Toast.LENGTH_LONG).show();

            }
        });

    }


}
